#!/usr/bin/evn python

import wsgiref.handlers
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.api import urlfetch


from models import *
from ptzmodels import *
from common.constants import *

import string

webapp.template.register_template_library('common.customtags')

class ErrorDet(webapp.RequestHandler):
    def get(self):
        from google.appengine.api import users
        from common.commonDB import authorizedToDeleteMesh, authorizedToDeleteCorrespondence
        import common.constants
        
        sceneId = self.request.get('scene')
        scene = Scene.get_by_key_name('scene' + str(sceneId))
        if scene:
            correspondences = Correspondence.all().filter('scene =', scene).fetch(limit=100)
            meshes = Mesh.all().filter('scene =', scene).fetch(limit=100)
            calibration = Calibration.get_by_key_name('calib' + str(sceneId));
        else:
            values = {'message': 'Scene not found.'}
            self.response.out.write(template.render(TEMPLATE_DIRECTORY + 'middle.html', values))
            return
        
        logInURL = users.create_login_url("/errorDetection?scene=" + str(sceneId))
        logOutURL = users.create_logout_url("/errorDetection?scene=" + str(sceneId))
        
        user = users.get_current_user()
        if not user:
            # make the user log in.
            
            values = {
                'user': user,
                'scene': None,
                'logInURL': logInURL,
                'logOutURL': logOutURL,
                }
            self.response.out.write(template.render(TEMPLATE_DIRECTORY + 'errorDetection.html', values))
            return          
    
        scene = Scene.get_by_key_name('scene' + str(sceneId))
        
    
        if scene.is_private:
            if user != scene.owner and user.nickname() not in ADMIN_EMAILS:
                values = {
                    'message': 'You do not have permission to view this scene. Click <a href="http://www.projectlive3d.com/">here</a> to return to the main page.',
                    'username' : user.nickname(),
                    'logInURL' : logInURL,
                    'logOutURL' : logOutURL
                }
                self.response.out.write(template.render(TEMPLATE_DIRECTORY + 'middle.html', values))
                return
        orphanCorrs = []
        for corr in correspondences:
            if corr.parentMeshes == 0:
                corr.deletable = authorizedToDeleteCorrespondence(corr, user)
                orphanCorrs.append(corr)
        
        for mesh in meshes:
            mesh.deletable = authorizedToDeleteMesh(mesh, user)
        
        admin = False
        if user.nickname() in ADMIN_EMAILS:
            admin = True
    
        values = {
                'user': user,
                'username': user.nickname(),
                'scene': scene,
                'pointCorrespondences' : orphanCorrs,
                'allCorrespondences' : correspondences,
                'meshes' : meshes,
                'calibration' : calibration,
                'logInURL': logInURL,
                'logOutURL': logOutURL,
                'admin': admin,
                }
        self.response.out.write(template.render(TEMPLATE_DIRECTORY + 'errorDetection.html', values))